package com.daniel.interview;

import lombok.extern.slf4j.Slf4j;

/**
 * @Description 用于面试展示的测试类
 * @Author GanShaoXi
 * @ClassName Test
 * @Return com.daniel.interview
 * @Since 1.0.0
 * @Created on 2023/5/22 18:29
 */
@Slf4j
public class Test {
    
    public static void main(String[] args) {
        int[] arr = new int[]{1, 3, 4, 6, 8, 9, 13, 22};
        int target = 4;
        search(arr, target);
        
        int[] arr1 = new int[]{9, 2, 7, 4, 3, 1, 5};
        bubbleSort(arr1);
    }
    
    // 二分查找
    public static void search(int[] arr, int target) {
        
        int left = 0;
        int right = arr.length - 1;
        
        while (left <= right) {
            log.info("left:{}, right:{}", left, right);
            int mid = (left + right) / 2;
            if (arr[mid] == target) {
                log.info("找到了目标值{}", target);
                return;
            } else if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        log.info("未找到目标值{} ", target);
    }
    
    // 冒泡排序方法
    public static void bubbleSort(int[] arr) {
        for (int i = 1; i <= arr.length; i++) {
            for (int j = 0; j < arr.length - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int item = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = item;
                }
            }
        }
        StringBuffer sb = new StringBuffer();
        sb.append("排序后的数组为：[");
        for (int i : arr) {
            sb.append(i).append(",");
        }
        sb.replace(sb.length() - 1, sb.length(), "]");
        log.info(sb.toString());
    }
}
